/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.core.defaultElements;

import java.awt.Point;
import java.awt.Polygon;
import java.util.ArrayList;

import plar.core.Action;
import plar.core.Element;
import plar.core.Parameters;

public class Elevator extends Element {

	class Attrito implements Action {

		@Override
		public Parameters call(Parameters parameters) {
			Element me = (Element) parameters.get("me");
			String subAction = (String) parameters.get("subAction");
			ArrayList<Element> collisions = (ArrayList<Element>) getProperty("collisionList");

			if (subAction == "main") {
				if (collisions != null)
					for (Element e : collisions) {
						if (e.getProperty("type") == "player") {

							Point relativeSpeed = new Point();
							Point eSpeed = e.getSpeed();
							relativeSpeed.x = me.getSpeed().x - eSpeed.x;
							relativeSpeed.y = me.getSpeed().y - eSpeed.y;

							if (relativeSpeed.x != 0)
								eSpeed.x += Math.abs(relativeSpeed.x)
										/ relativeSpeed.x;
							if (relativeSpeed.y != 0)
								eSpeed.y += Math.abs(relativeSpeed.y)
										/ relativeSpeed.y;
							e.setSpeed(eSpeed);

						}
					}

			}
			return null;

		}

	}
	public class ElevatorMove implements Action {
		public ElevatorMove() {
		}

		@Override
		public Parameters call(Parameters parameters) {

			String subaction = (String) parameters.get("subAction");
			Element me = (Element) parameters.get("me");
			Polygon polygon = (Polygon) getProperty("path");
			Integer xspeed = (Integer) getProperty("xspeed");
			Point speed = (Point) me.getProperty(".speed");
			Point position = me.getPosition();
			if (position.x > polygon.xpoints[1])
				speed.x = -xspeed;
			else if (position.x < polygon.xpoints[0])
				speed.x = xspeed;
			// else speed.x=xspeed;

			setProperty(".speed", speed);
			return parameters;
		}

	}

	Parameters info;

	ArrayList<ArrayList<String>> keysQueue;

	public Elevator() {
		super();

		setProperty("type", "elevator");
		setProperty("isRunnable", true);
		setProperty("canCollide", true);
		setProperty("canStop", false);
		setProperty("xspeed", 3);
		setSize(new Point(130, 30));
		setProperty("path", new Polygon());
		actions.addAction("move", new ElevatorMove());
		actions.addAction("collision", new Attrito());
	}

	public void run() {
		super.run();

		ArrayList<Element> collisions = new ArrayList<Element>();
		Point oldspeed = new Point(0, -1);

		collisions = level.getCollisions(this, getTraceRectangle(oldspeed));
		setProperty("collisionList", collisions);
		actions.runAction("collision", stdinfo);

	}

}
